package app.auto.runner.base.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import app.auto.runner.base.DipUtil;
import app.auto.runner.base.MapAdapter;

public class BFlowLayout extends AniLayout {
    List<View> set = new ArrayList<View>(7);
    int marginHorizontal;
    int marginVertical;
    private float margin = 5;

    public BFlowLayout setMargin(float margin) {
        this.margin = margin;
        return this;
    }

    public void addCollection(Collection<View> all) {
        for (View view : all) {
            addView(view);
        }
    }

    public BFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        marginHorizontal = DipUtil.fromDip(context, 16);
        marginVertical = DipUtil.fromDip(context, 8);
    }

    int y;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        int x = 0;

        int row = 0;
        if (mAdapter != null) {
            y = 0;
            for (int index = 0; index < mAdapter.getCount(); index++) {
                View child = null;

                if (set.size() - 1 >= index) {
                    child = set.get(index);
                    child = mAdapter.getView(index, child, null);
                } else {
                    child = mAdapter.getView(index, null, null);
                    set.add(child);
                    MarginLayoutParams ml = new MarginLayoutParams(-2, -2);
                    ml.leftMargin = DipUtil.fromDip(margin);
                    ml.topMargin = DipUtil.fromDip(margin);

                    addView(child, ml);
                }
                if (child.getVisibility() != View.GONE) {
                    child.measure(View.MeasureSpec.UNSPECIFIED,
                            View.MeasureSpec.UNSPECIFIED);
                    int width = child.getMeasuredWidth() + marginHorizontal;
                    int height = child.getMeasuredHeight() + marginVertical;
                    x += width;
                    y = row * height + height;
                    if (x > maxWidth) {
                        x = width;
                        row++;
                        y = row * height + height;
                    }
                }
            }
            setMeasuredDimension(maxWidth, y);
            return;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void clear() {
        set.clear();
        removeAllViews();
        invalidate();
        mAdapter.clearDataSrc();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childCount = getChildCount();
        int maxWidth = r - l;
        int x = 0;
        int y = 0;
        int row = 0;

        for (int i = 0; i < childCount; i++) {
            final View child = this.getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth() + marginHorizontal;
                int height = child.getMeasuredHeight() + marginVertical;
                x += width;
                y = row * height + height;
                if (x > maxWidth) {
                    x = width;
                    row++;
                    y = row * height + height;
                }
                child.layout(x - width, y - height, x, y);

            }
        }

    }

    public MapAdapter getAdapter() {
        // TODO Auto-generated method stub
        return mAdapter;
    }


    MapAdapter mAdapter = new MapAdapter(this.getContext());

    public void setAdapter(MapAdapter adapter) {
        // TODO Auto-generated method stub
        mAdapter = adapter;

        requestLayout();
    }
}